package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.HrouteDao;
import model.Hroute;
import util.CharSet;

/**
 * Servlet implementation class HrouteServlet
 */
@WebServlet("/HrouteServlet")
public class HrouteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HrouteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	//	/response.getWriter().append("Served at: ").append(request.getContextPath());
		// TODO Auto-generated method stub
				// response.getWriter().append("Served at: ").append(request.getContextPath());
				response.setContentType("text/html;charset=utf-8");
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				String handle = request.getParameter("handle");
				if (handle != null && handle.equals("delHroute")) {
					String hemail = request.getParameter("hemail");
					String rname = request.getParameter("rname");
					Hroute hroute;
					try {
						hroute = HrouteDao.newHroute(rname, hemail);
						if (HrouteDao.getHrouteNoCacheForBool(hroute)) {
							HrouteDao.delHroute(hroute);
							String flag = "1";
							request.setAttribute("flag", flag);
							request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
						} else {
							String flag = "0";
							request.setAttribute("flag", flag);
							request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
						}
					} catch (Exception e) {
						String flag = "0";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (handle != null && handle.equals("addHroute")) {
					String hemail = CharSet.getNewString(request.getParameter("hemail"));
					System.out.println("ggg"+hemail);
					String rname = CharSet.getNewString(request.getParameter("rname"));
					System.out.println("ggg= "+rname);
					Hroute hroute;
					try {
						//给对象赋值
						hroute = HrouteDao.newHroute(rname, hemail);
						System.out.println(hroute.getId().getHEmail()+hroute.getId().getRName());
						if (!HrouteDao.getHrouteNoCacheForBool(hroute)) {
							HrouteDao.addHroute1(hroute);
							if (HrouteDao.getHrouteNoCacheForBool(hroute)) {
								String flag = "1";
								request.setAttribute("flag", flag);
								request.getRequestDispatcher("/admin/AdminAddHroutList.jsp").forward(request, response);
							} else {
								String flag = "0";
								request.setAttribute("flag", flag);
								request.getRequestDispatcher("/admin/AdminAddHroutList.jsp").forward(request, response);
							}

						} else {
							String flag = "0";
							request.setAttribute("flag", flag);
							request.getRequestDispatcher("/admin/AdminAddHroutList.jsp").forward(request, response);
						}
					} catch (Exception e) {
						String flag = "0";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminAddHroutList.jsp").forward(request, response);
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
